import api from '@/http/api.js';
import util from '@/http/util.js';
export default {
	data() {
		return {
			BaseUrl:getApp().globalData.apiUrl,
			formData: {
				qrcode: '', // 实际项目中替换为二维码图片URL
				alipayAccount: '',
				alipayName: '',
				bankName: '',
				accountOpeningArea: '', //开户地区
				accountOpeningBranch: '', //开户网点
				city: '',
				nickname: '',
				dailyOrders: '',
				concurrentOrders: ''
			},

			choosePayId: "", //渠道id
			choosePayName: "", //渠道名称

			showCityPicker: false,
			cities: []
		};
	},
	onLoad(data) {
		console.log(data)
		if (data) {
			this.choosePayId = data.id;
			this.choosePayName = data.name;
		}
		try{
			let that = this;
			uni.getStorage({
				key:'edit',
				success(res) {
					console.log(res.data)
					that.formData.qrcode = res.data.qr_code,
					that.formData.alipayAccount = res.data.bank_card,
					that.formData.alipayName = res.data.real_name,
					that.formData.bankName = res.data.bank_name,
					that.formData.accountOpeningArea = res.data.bank_area,
					that.formData.accountOpeningBranch = res.data.bank_branch,
					that.formData.nickname = res.data.nickname,
					that.formData.dailyOrders = res.data.one_day_order,
					that.formData.concurrentOrders = res.data.tong_shi_order
					uni.removeStorage({
						key: 'edit'
					});
				}
			})
		}catch(e){
			console.log(e)
		}
	},
	methods: {
		//上传图片
		uploadPictures() {
			let that = this;
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				sourceType: ['album'],
				success: function(res) {
					uni.uploadFile({
						url: 'https://4pay.dt12.cfd/api/common/upload',
						filePath: res.tempFilePaths[0],
						header: {
							'token': util.getToken(),
						},
						success(res) {
							let newres = JSON.parse(res.data)
							if (newres.code == 1) {
								that.formData.qrcode = newres.data.url
								uni.showToast({
									title: '上传成功'
								})
							}else{
								uni.showToast({ title: res.msg, icon: 'none' });
							}
						},
						fail(err) {
							uni.showToast({
								title: '上传失败',
								icon: 'error'
							})
						}
					})
				}
			})
		},
		// selectCity(province, city) {
		//   this.formData.city = `${province}/${city}`;
		//   this.showCityPicker = false;
		// },

		saveAccount() {
			api.addPaymentMethod({
				"bank_type_id": this.choosePayId,
				"bank_name": this.formData.bankName,
				"bank_area": this.formData.accountOpeningArea,
				"bank_branch": this.formData.accountOpeningBranch,
				"bank_card": this.formData.alipayAccount,
				"real_name": this.formData.alipayName,
				"qr_code": this.formData.qrcode,
				"nickname": this.formData.nickname,
				"one_day_order": this.formData.dailyOrders,
				"tong_shi_order": this.formData.concurrentOrders
			}).then(res => {
				if (res.code == 1) {
					uni.showToast({
						title: '账号信息保存成功',
						icon: 'success'
					});
					setTimeout(()=>{
						uni.navigateBack({delta:1})
					},1500)
				}else{
					uni.showToast({title:res.msg,icon:'error'})
				}
			}).catch(err => {

			})


			
			console.log('保存的数据:', this.formData);
		}
	}
};